package com.example.hxpc.guidancemanagement.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.DialogUtil;
import com.example.hxpc.guidancemanagement.tools.LogoutT;
import com.example.hxpc.guidancemanagement.tools.SharePreferenceTools;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

/**
 * Created by tao on 2017/12/5.
 */

public class ChangeNameActivity extends Activity implements View.OnClickListener,BaseApi{
    private ImageView img_add , img_back;
    private TextView te_title , te_sure;
    private OkHttpTools okHttpTools = new OkHttpTools();
    private EditText edt_change_name;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_change_name);
        initView();
    }
    //初始化控件
    public void initView(){
        edt_change_name = (EditText) findViewById(R.id.edt_change_name);
        img_add = (ImageView) findViewById(R.id.add_mark);
        img_add.setVisibility(View.GONE);
        img_back = (ImageView) findViewById(R.id.back);
        img_back.setOnClickListener(this);
        te_title = (TextView) findViewById(R.id.te_title);
        te_title.setText("修改昵称");
        te_sure = (TextView) findViewById(R.id.sure);
        te_sure.setVisibility(View.VISIBLE);
        te_sure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sure:
                if (edt_change_name.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请输入新昵称");
                }else{
                    okHttpTools.changeName(this, SharePreferenceTools.getInstance(this).getString("uid"),edt_change_name.getText().toString());
                }
                break;
            case R.id.back:
                Intent intent = new Intent();
                intent.putExtra("name", SharePreferenceTools.getInstance(this).getString("name"));
                setResult(0,intent);
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            Intent intent = new Intent();
            intent.putExtra("name", SharePreferenceTools.getInstance(this).getString("name"));
            setResult(0,intent);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase) {
        if (success.equals("true")){
            DialogUtil.show(this, R.string.logining);
            SharePreferenceTools.getInstance(this).setString("name",edt_change_name.getText().toString());
            DialogUtil.dismiss2Msg("修改中。。。");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent();
                    intent.putExtra("name",edt_change_name.getText().toString());
                    setResult(0,intent);
                    finish();
                    ToastTools.toastMsg(ChangeNameActivity.this,"修改成功");
                }
            },2500);
        }else{
            ToastTools.toastMsg(this,jsonBase.getMsg());
        }
    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {

    }
}
